﻿var Popups = function (stringContainerId) {
    this.theme = "/css/Popups.css";
    this.maskTheme = "/css/Mask.css";
    this.intMainHeight = 190;
    this.intMainWidth = 489;
	this.containerElement = getElement(stringContainerId);
	this.containerElement.onclick = function () {
		var event = arguments[0] || window.event;
		event.cancelBubble = true;
	};
	this.bindElement = function (stringTagName, stringClassName, objectContainerElement, objectContainer) {
		var element = createElement(stringTagName);
		element.className = stringClassName;
		if (objectContainerElement) {
			objectContainerElement.appendChild(element);
		}
		if (objectContainer){
			element.container = objectContainer;
		}
		return element;
	};
	this.initializeElement = function (stringTitleIconClassName, stringIconClassName, booleanButtons, functionCallback, buttonText) {
	    
	    if( $("popupsCss") == null )
	    {
	        OnStylesheetInit(this.theme, "popupsCss");
	    }
	    
		var textOK = "确定";
		var textCancel = "取消";
		
		if (buttonText) {
			textOK = buttonText[0];
			if (buttonText[1]) {
				textCancel = buttonText[1];
			}
		}
		var container = this.bindElement("div", "popup");
		container.style.width = this.intMainWidth + "px";
		container.style.height = this.intMainHeight + "px";
		container.bindElement = this.bindElement;
		var shadow = this.bindElement("div", "shadow", container, container);
		shadow.style.width = (this.intMainWidth + 2) + "px";
		shadow.style.height = (this.intMainHeight + 2) + "px";
		var main = this.bindElement("div", "main", container, container);
		main.style.width = this.intMainWidth + "px";
		var head = this.bindElement("div", "head", main, container);
		head.style.width = this.intMainWidth + "px";
		container.head = head;
		var titleIcon = this.bindElement("div", stringTitleIconClassName, head, container);
		container.titleIcon = titleIcon;
		var tail = this.bindElement("div", "tail", head, container);
		var closeIcon = this.bindElement("div", "closeIcon", tail, container);
		closeIcon.backgroundElement = tail;
		closeIcon.onmouseover = function () {
			this.backgroundElement.className = "tailH";
		};
		closeIcon.onmouseout = function () {
			this.backgroundElement.className = "tail";
		};
		closeIcon.onclick = function () {
			this.container.remove();
		};
		var body = this.bindElement("div", "body", main, container);
		body.style.width = this.intMainWidth + "px";
		var icon = this.bindElement("div", stringIconClassName, body, container);
		container.icon = icon;
		var element = this.bindElement("div", "element", body, container);
		var elementTable = createElement("table");
		elementTableTrTd = elementTable.insertRow(0).insertCell(0);
		elementTableTrTd.className = "elementEntity";
		element.appendChild(elementTable);
		container.element = elementTableTrTd;
		var buttons = this.bindElement("div", "buttons", body, container);
		container.buttons = buttons;
		var buttonOk = this.bindElement("input", "button");
		buttonOk.className = "btn";
		buttonOk.type = "button";
		buttonOk.value = textOK;
		buttonOk.container = container;
		buttonOk.onmouseover = function()
		{
		    this.className = "btnOver";
		}
		buttonOk.onmouseout = function()
		{
		    this.className = "btn";
		}
		buttonOk.onclick = function () {
			if (this.container.input) {
				if (this.container.input.value == "") {
					popups.createPopup("AlertError", "输入不能为空。").show();
					this.container.input.focus;
				}
				else {
					var isClose = functionCallback(this.container.input.value);
					if( typeof(isClose) == 'undefined' || isClose == null || isClose == true )
					{
					    this.container.remove();
					}
				}
			}
			else {
				var isClose = functionCallback(this.container);
				if( typeof(isClose) == 'undefined' || isClose == null || isClose )
				{
				    this.container.remove();
				}
			}
		};
		container.buttonOk = buttonOk;
		container.maskTheme = this.maskTheme;
		buttons.appendChild(buttonOk);
		if (booleanButtons) {
			var buttonCancel = this.bindElement("input", "button");
			buttonCancel.className = "btn";
			buttonCancel.type = "button";
			buttonCancel.value = textCancel;
			buttonCancel.container = container;
		    buttonCancel.onmouseover = function()
		    {
		        this.className = "btnOver";
		    }
		    buttonCancel.onmouseout = function()
		    {
		        this.className = "btn";
		    }			
			buttonCancel.onclick = function () {
				this.container.remove();
			};
			container.buttonCancel = buttonCancel;
			buttons.appendChild(buttonCancel);
		}
		this.containerElement.appendChild(container);
		hideElement(container);
		container.show = function () {
			this.style.left = (getWindowSize()[0] - 489) / 2 + document.documentElement.scrollLeft + "px";
			this.style.top = (getWindowSize()[1] - 192) / 2 + document.documentElement.scrollTop + "px";
			try
			{
			    if( typeof(mask) == "undefined" && typeof(Mask) != "undefined" )
			    {
			        window.mask = new Mask();
			        window.mask.theme = this.maskTheme;
			    }
			    if( window.mask )
			    {
			        window.mask.show();
			        this.style.zIndex = mask.zIndexNow;
			    }
			}
			catch(e)
			{}
			showElement(this);
			if( DragAble )
			{
			    var dragAble = new DragAble(this, this.head);
			}
			if (this.input) {
				var setTimeoutFocus = function () {
					arguments.callee.input.select();
				};
				setTimeoutFocus.input = this.input;
				setTimeout(setTimeoutFocus, 1);
			}
			else {
				this.buttonOk.focus();
			}
		};
		container.remove = function () {
		    try
		    {
			    mask.clean();
			}
			catch(e)
			{}
			removeElement(this);
		};
		container.hide = function () {
			hideElement(this);
		};
		return container;
	};
	
	this.bindText = function (stringText, stringClassName, objectContainerElement) {
		var element = createElement("label");
		element.className = stringClassName;
		element.innerHTML = stringText;
		if (objectContainerElement) {
			objectContainerElement.appendChild(element);
		}
		return element;
	};
	this.createPopup = function (stringType, stringNote, functionCallback) {
		switch (stringType) {
			case "AlertError":
				var popup = this.initializeElement("titleAlert", "iconWarning", false, new Function());
				popup.element.innerHTML = '<div class="PopupTitle">' + stringNote + '</div>';
				return popup;
				break;
			case "AlertSuccess":
				var popup = this.initializeElement("titleAlert", "iconSuccess", false, new Function());
				popup.element.innerHTML = '<div class="PopupTitle">' + stringNote + '</div>';
				return popup;
				break;
			case "SetMyFund":
			    var popup = this.initializeElement("titleAlert", "iconWarning", false, new Function());
			    popup.element.innerHTML = '<div class="PopupTitle">' + stringNote + '</div><br />';
			    popup.element.innerHTML += "“查看我关注的基金”是需要成为数米网会员才有的功能，所以您可能没有<a href='http://user.fund123.cn/register.aspx'><b><u>注册</u></b></a>或者没有<a href='http://user.fund123.cn/login.aspx'><b><u>登陆</u></b></a>数米网，或者你登录了，但没有设置我关注的基金，<a href='http://my.fund123.cn/SetMyFavoriteFund.aspx'><b><u>点此链接</u></b></a>设置。"; 
			    return popup;
			    break;
		};
	};
};